package com.cwt.chat.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cwt.chat.model.MsgVo;
import com.cwt.chat.model.WeChatLog;
import com.cwt.chat.service.WeChatLogService;
import com.cwt.common.result.AjaxResult;
import com.cwt.common.utils.SqlUtils;
import com.cwt.common.vo.PageInfo;
import com.google.common.collect.Lists;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author chenwt
 * @version V1.0
 * @description 【聊天日志接口管理】
 * @since 2023年11月10日 11:32:44
 */
@Api(tags = "聊天日志", value = "ChatLogController")
@RequestMapping("/chatLog")
@RestController
public class ChatLogController {

    @Autowired
    private WeChatLogService weChatLogService;

    /**
     * 图书分页查询
     *
     * @param info 分页查询条件
     * @return 图书分页数据信息
     */
    @RequestMapping(value = "/page", method = {RequestMethod.POST, RequestMethod.GET})
    public @ResponseBody
    AjaxResult page(@RequestBody PageInfo info) {
        Page<WeChatLog> page = new Page<>(info.getPageNum(), info.getPageSize());
        QueryWrapper<WeChatLog> queryWrapper = new QueryWrapper<>();
        SqlUtils.wrapperSetLikeCondition(queryWrapper, info.getParams());
        queryWrapper.orderByDesc("create_time"); // 根据时间降序
        queryWrapper.eq("roomname", "房间号"); // 设置查询的聊天室，根据聊天室查询聊天记录
        page = weChatLogService.page(page, queryWrapper);

        // 配置相应数据信息
        List<MsgVo> msgs = new ArrayList<>();
        page.getRecords().forEach(chatLog -> {
            msgs.add(MsgVo.builder()
                    .username(chatLog.getUsername())
                    .msg(chatLog.getMsg())
                    .userId(chatLog.getUserId())
                    .avatar(chatLog.getAvatar())
                    .build());
        });
        return AjaxResult.success(new Page<MsgVo>().setTotal(page.getTotal()).setRecords(msgs));
    }

}
